/**
 *
 *
 * @export
 * @param {number} num
 * @returns
 * @description echarts y轴最大值取整
 */
export function roundToNearestMultiple(num: number) {
  if (num >= 10000) {
    return Math.ceil(num / 10000) * 10000
  } else if (num >= 1000) {
    return Math.ceil(num / 1000) * 1000
  } else if (num >= 100) {
    return Math.ceil(num / 100) * 100
  } else if (num >= 10) {
    return Math.ceil(num / 10) * 10
  } else {
    return num // 如果输入小于10，则直接返回该数字本身
  }
}

export function getStepRound(num: number) {
  if (num <= 5) return 5
  if (num <= 10) return 10
  if (num < 1000) return roundToNearestBoundary(num, 2)
  return roundToNearestBoundary(num, 5)
}
// 分段向上取整取近似值，setp为2时分为两段如：传入值为127会取150，178会取200；step为5时分五段：传入112时取120，134取140以此类推
function roundToNearestBoundary(value: number, step: number): number {
  if (typeof value !== 'number' || isNaN(value)) {
    throw new Error('value must be a valid number')
  }

  const numLength = String(value).split('.')[0].length
  const minByNumLength = Array.from({ length: numLength }, (_, i) =>
    i === 0 ? '1' : '0'
  ).join('')
  const splitNum = Number(minByNumLength) / step
  // 计算最近的边界值
  const lowerBound =
    Math.floor(value / Number(minByNumLength)) * Number(minByNumLength)
  let upperBound = lowerBound + splitNum

  // 检查是否在指定的范围内
  if (value === lowerBound) {
    return lowerBound
  }
  for (let i = 0; i < step; i++) {
    if (value <= upperBound) {
      return upperBound
    }
    upperBound += splitNum
  }
  // 用作类型校验，必不可能执行
  return value
}
